package com.tt.service.impl;

import com.tt.entity.bo.AllProvincePieBO;
import com.tt.entity.bo.NameValueBO;
import com.tt.entity.vo.AllProvincePieVO;
import com.tt.mapper.RawApparentTotalMapper;
import com.tt.service.AllProvincePieService;
import com.tt.util.ConstUtil;
import com.tt.util.HandleDecimal;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;

/**
 * <p>
 *
 * </p>
 *
 * @version 1.0
 * @auther sugarttt
 * @since 2022-11-10 10:34:35
 */
@Service
public class AllProvincePieServiceImpl implements AllProvincePieService {

    private final RawApparentTotalMapper rawApparentTotalMapper;

    public AllProvincePieServiceImpl(RawApparentTotalMapper rawApparentTotalMapper) {
        this.rawApparentTotalMapper = rawApparentTotalMapper;
    }

    @Override
    public ArrayList<AllProvincePieVO> getAllProvincePie(String beginYear, String endYear) {
        int diff = Integer.parseInt(endYear) - Integer.parseInt(beginYear) + 1;
        ArrayList<AllProvincePieVO> allProvincePieVOS = new ArrayList<>();

        ArrayList<AllProvincePieBO> allProvincePieBOS = rawApparentTotalMapper.getAllProvincePie(beginYear, endYear);
        Iterator<AllProvincePieBO> iterator = allProvincePieBOS.iterator();

        ArrayList<NameValueBO> nameValueBOS;
        AllProvincePieBO allProvincePieBO;
        // 全国时， 需要将每个省每年的值加起来
//        if (provinceId == 0) {
        double sumCoalValue = 0.0;
        double sumOilValue = 0.0;
        double sumGasValue = 0.0;
        double sumCement = 0.0;

        for (int id = diff; iterator.hasNext(); ) {
            allProvincePieBO = iterator.next();

            if (id <= diff) {
                sumCoalValue += Double.parseDouble(allProvincePieBO.getCoalValue());
                sumOilValue += Double.parseDouble(allProvincePieBO.getOilValue());
                sumGasValue += Double.parseDouble(allProvincePieBO.getNaturalGasValue());
                sumCement += Double.parseDouble(allProvincePieBO.getCement());
            }

            --id;

            if (id == 0) {
                nameValueBOS = new ArrayList<>();
                nameValueBOS.add(new NameValueBO(ConstUtil.RAW_COAL, HandleDecimal.handle(sumCoalValue)));
                nameValueBOS.add(new NameValueBO(ConstUtil.CRUDE_OIL, HandleDecimal.handle(sumOilValue)));
                nameValueBOS.add(new NameValueBO(ConstUtil.NATURAL_GAS, HandleDecimal.handle(sumGasValue)));
                nameValueBOS.add(new NameValueBO(ConstUtil.CEMENT, HandleDecimal.handle(sumCement)));
                allProvincePieVOS.add(new AllProvincePieVO(
                        allProvincePieBO.getProvinceName(),
                        HandleDecimal.handle(
                                HandleDecimal.handle(sumCoalValue) +
                                        HandleDecimal.handle(sumOilValue) +
                                        HandleDecimal.handle(sumGasValue) +
                                        HandleDecimal.handle(sumCement)),
                        nameValueBOS));

                // next province
                sumCoalValue = 0.0;
                sumOilValue = 0.0;
                sumGasValue = 0.0;
                sumCement = 0.0;
                id = diff;
            }
        }
        // 具体某个省时，只需要处理一下数据
//        } else {
//
//            while (iterator.hasNext()) {
//                allProvincePieBO = iterator.next();
//                nameValueBOS = new ArrayList<>();
//                nameValueBOS.add(new NameValueBO(ConstUtil.RAW_COAL, HandleDecimal.handle(allProvincePieBO.getCoalValue())));
//                nameValueBOS.add(new NameValueBO(ConstUtil.CRUDE_OIL, HandleDecimal.handle(allProvincePieBO.getOilValue())));
//                nameValueBOS.add(new NameValueBO(ConstUtil.NATURAL_GAS, HandleDecimal.handle(allProvincePieBO.getNaturalGasValue())));
//                nameValueBOS.add(new NameValueBO(ConstUtil.CEMENT, HandleDecimal.handle(allProvincePieBO.getCement())));
//
//                allProvincePieVOS.add(new AllProvincePieVO(allProvincePieBO.getProvinceName(), nameValueBOS));
//            }
//        }

        // 排序 ASC
        allProvincePieVOS.sort(Comparator.comparing((allProvincePieVO) -> allProvincePieVO.getValue().get(0).getValue() +
                allProvincePieVO.getValue().get(1).getValue() +
                allProvincePieVO.getValue().get(2).getValue() +
                allProvincePieVO.getValue().get(3).getValue()));

        return allProvincePieVOS;
    }
}
